package com.server.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.model.all.enums.YesNoEnum;
import com.server.common.code.error.BusinessException;
import com.server.common.code.utils.OptionalUtils;
import com.server.model.all.user.entity.UserRoleEntity;
import com.server.user.mapper.UserRoleMapper;
import com.server.user.service.UserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleEntity> implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Long> getUserRoleByUserId(Long userId) {
        //获取角色集合
        List<UserRoleEntity> userRoleEntityList = userRoleMapper.selectList(new LambdaQueryWrapper<UserRoleEntity>()
                .eq(UserRoleEntity::getUserId, userId)
                .eq(UserRoleEntity::getDelFlag, YesNoEnum.NO.getCode()));

        OptionalUtils.ofEmpty(userRoleEntityList).orElseThrow(()->new BusinessException("该用户没有角色!"));

        return userRoleEntityList.stream().map(UserRoleEntity::getUserRoleId).collect(Collectors.toList());
    }
}
